﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PluginFramework.Common;
using QLBH.FeatureBase.Modules;
using QLBH.FeatureBase.Modules.Category;
using QLBH.FeatureBase.Modules.System;
using QLBH.FeatureBase.Modules.MuaBanHang;
using QLBH.FeatureBase;
using QLBH.FeatureBase.Modules.CongNo;
using QLBH.ConfigLib;
using QLBH.Model.Controller;

namespace QLBH
{    
    public partial class QLBH : Office2007RibbonForm
    {
        private ButtonItem oldButton;
        private ButtonItem currentBtn;
        public QLBH()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }
        private void ChangButttonState(ButtonItem btn)
        {
            oldButton = currentBtn;
            currentBtn = btn;
            if (oldButton!=null)
            {
                oldButton.Checked = false;
            }
            currentBtn.Checked = true;
        }

        private void btnQLKhoHang_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.Category.QLKhoTool";

            IModule moduleTool = (IModule)(new QLKhoToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                tableLayoutPanel1.Controls.Add(moduleTool.GetView());

                ChangButttonState(btnQLKhoHang);
            }
        }
        private void btnQLNhomHang_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.Category.QLNhomHangTool";

            IModule moduleTool = (IModule)(new QLNhomHangToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                tableLayoutPanel1.Controls.Add(moduleTool.GetView());

                ChangButttonState(btnQLNhomHang);
            }
        }

        private void btnQLNganhHang_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.Category.QLNganhHangTool";

            IModule moduleTool = (IModule)(new QLNganhHangToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                tableLayoutPanel1.Controls.Add(moduleTool.GetView());

                ChangButttonState(btnQLNganhHang);

            }
        }

        private void btnQLMatHang_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.Category.QLMatHangTool";

            IModule moduleTool = (IModule)(new QLMatHangToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                tableLayoutPanel1.Controls.Add(moduleTool.GetView());

                ChangButttonState(btnQLMatHang);

            }
        }

        private void btnDVT_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.Category.QLDVTTool";

            IDialogModule moduleTool = (IDialogModule)(new QLDVTToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                moduleTool.GetDialog().ShowDialog();
            }
        }

        private void btnLyDo_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.Category.QLLyDoTool";

            IDialogModule moduleTool = (IDialogModule)(new QLLydoExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                moduleTool.GetDialog().ShowDialog();
            }
        }

        private void QLNhanVien_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.Category.QLNhanVienTool";

            IModule moduleTool = (IModule)(new StaffManagementToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                tableLayoutPanel1.Controls.Add(moduleTool.GetView());

                ChangButttonState(QLNhanVien);

            }
        }

        private void btnQLNCC_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.Category.QLNhaCungCapTool";

            IModule moduleTool = (IModule)(new ProviderManagementToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                tableLayoutPanel1.Controls.Add(moduleTool.GetView());

                ChangButttonState(btnQLNCC);

            }
        }

        private void btnQLKhachHang_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.Category.QLKhachHangTool";

            IModule moduleTool = (IModule)(new CustomerManagementToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                tableLayoutPanel1.Controls.Add(moduleTool.GetView());

                ChangButttonState(btnQLKhachHang);

            }
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            String toolName = "Module.SystemUI.ConfigurationTool";

            IFormModule moduleTool = (IFormModule)(new ConfigurationToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                moduleTool.GetForm().ShowDialog();
            }
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnConfigSystem_Click(object sender, EventArgs e)
        {
            String toolName = "Module.SystemUI.ConfigurationTool";

            IFormModule moduleTool = (IFormModule)(new ConfigurationToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                moduleTool.GetForm().ShowDialog();
            }
        }

        private void btnBackUpanRestore_Click(object sender, EventArgs e)
        {
            String toolName = "Module.SystemUI.BackupTool";

            IFormModule moduleTool = (IFormModule)(new ConfigurationToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                moduleTool.GetForm().ShowDialog();
            }
        }

        private void btnDonMuaHang_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.SaleProduct.DSDonMuaHangTool";
            IModule moduleTool = (IModule)(new DSPhieuMuaHangToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                tableLayoutPanel1.Controls.Add(moduleTool.GetView());

                ChangButttonState(btnDonMuaHang);

            }
            
            
        }

        private void btnHDBanHang_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.SaleProduct.BanHangTool";
            IModule moduleTool = (IModule)(new BanHangToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                FormViewBase uc = moduleTool.GetView();
                tableLayoutPanel1.Controls.Add(uc);

                ChangButttonState(btnHDBanHang);

            }
        }

        private void QLBH_KeyDown(object sender, KeyEventArgs e)
        {
            if (tableLayoutPanel1.Controls.Count>0)
            {
                FormViewBase fv = (FormViewBase)tableLayoutPanel1.Controls[0];
                fv.OnKeyDown(e);
            }
        }

        private void QLBH_Load(object sender, EventArgs e)
        {
            QLBHSettings setting = QLBHSettings.Instance;
            tsslbServerAddress.Text = setting.ServerName;
            OnLogin();
            setting = QLBHSettings.Instance;
            tsslbServerAddress.Text = setting.ServerName;
        }
        private void OnLogin()
        {
            this.Cursor = Cursors.WaitCursor;
            String toolName = "Module.SystemUI.LoginTool";
            IFormModule moduleTool = (IFormModule)(new LoginToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                if (moduleTool.GetForm().ShowDialog() == DialogResult.OK)
                {
                    //Login success
                    if (AccountHandler.GetCurrentUserLogin()!=null)
                    {
                        tssLbUserName.Text = AccountHandler.GetCurrentUserLogin().TENNV;
                    }                                                                                       
                }
                else
                {
                    this.Close();
                }

            }
            this.Cursor = Cursors.Default;
        }

        private void btnUserManagement_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            String toolName = "Module.SystemUI.UserManagementTool";
            IFormModule moduleTool = (IFormModule)(new UserManagementExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                moduleTool.GetForm().ShowDialog();
            }
            this.Cursor = Cursors.Default;
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            OnLogin();
        }

        private void btnNhapHang_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.SaleProduct.NhapKhoTool";
            IModule moduleTool = (IModule)(new NhapKhoToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                FormViewBase uc = moduleTool.GetView();
                tableLayoutPanel1.Controls.Add(uc);

                ChangButttonState(btnNhapHang);
            }
        }

        private void btnKHTraLai_Click(object sender, EventArgs e)
        {
            String toolName = "QLBH.SaleProduct.NhapKhoTool";
            IModule moduleTool = (IModule)(new NhapKhoToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));

            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                FormViewBase uc = moduleTool.GetView();
                tableLayoutPanel1.Controls.Add(uc);

                ChangButttonState(btnKHTraLai);
            }
        }

        private void btnCalGiaBan_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            String toolName = "QLBH.SaleProduct.CalcGiaBanTool";
            IFormModule moduleTool = (IFormModule)(new CalGiabanToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));
            if (moduleTool!=null)
            {
                moduleTool.GetForm().ShowDialog();
            }
            this.Cursor = Cursors.Default;
        }

        private void btnPhieuThu_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            String toolName = "QLBH.CongNo.PhieuThuChiTool";
            IModule moduleTool = (IModule)(new PhieuThuChiExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));
            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                FormViewBase uc = moduleTool.GetView(false);// false is  phieu thu
                tableLayoutPanel1.Controls.Add(uc);

                ChangButttonState((ButtonItem)sender);
            }
            this.Cursor = Cursors.Default;
        }

        private void btnPhieuChi_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            String toolName = "QLBH.CongNo.PhieuThuChiTool";
            IModule moduleTool = (IModule)(new PhieuThuChiExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));
            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                FormViewBase uc = moduleTool.GetView(true);// true is phieu chi
                tableLayoutPanel1.Controls.Add(uc);

                ChangButttonState((ButtonItem)sender);
            }
            this.Cursor = Cursors.Default;
        }

        private void btnSoNo_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            String toolName = "QLBH.CongNo.SoNoTool";
            IModule moduleTool = (IModule)(new SoNoExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));
            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                FormViewBase uc = moduleTool.GetView();
                tableLayoutPanel1.Controls.Add(uc);

                ChangButttonState((ButtonItem)sender);
            }
            this.Cursor = Cursors.Default;
        }

        private void btnRegisterInformation_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            String toolName = "Module.SystemUI.RegisterInformationTool";
            IFormModule moduleTool = (IFormModule)(new RegisterInfomationToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));
            if (moduleTool != null)
            {
                moduleTool.GetForm().ShowDialog();
            }
            this.Cursor = Cursors.Default;
        }

        private void btnPhieuChuyenKho_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            String toolName = "QLBH.Category.ChuyenKhoTool";
            IModule moduleTool = (IModule)(new PhieuChuyenKhoToolExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));
            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                FormViewBase uc = moduleTool.GetView();
                tableLayoutPanel1.Controls.Add(uc);

                ChangButttonState((ButtonItem)sender);
            }
            this.Cursor = Cursors.Default;
        }

        private void ribbonTabItem2_Click(object sender, EventArgs e)
        {

        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {

        }

        private void btnDoanhThu_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            String toolName = "QLBH.CongNo.DoanhThuTool";
            IModule moduleTool = (IModule)(new DoanhThuExtensionPoint()).CreateExtension(
                new ClassNameExtensionFilter(toolName));
            if (moduleTool != null)
            {
                tableLayoutPanel1.Controls.Clear();
                FormViewBase uc = moduleTool.GetView();
                tableLayoutPanel1.Controls.Add(uc);

                ChangButttonState((ButtonItem)sender);
            }
            this.Cursor = Cursors.Default;
        }
    }   
}
